<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php include('menu.php'); ?>
</head>
<body>
<?php
include("../connect/connect.php");

//ลบข้อมูล
if($_GET["action"]==del) {

	$Query = "DELETE FROM user WHERE user_id = '".$_GET["id"]."'";
	$objQuery = mysql_query($Query);
	if(!$objQuery)
	{
		echo "Error Delete [".mysql_error()."]";
	}
}
//ค้นหาพนักงาน
if($_POST["firstname_search"] != "" OR $_POST["lastname_search"] != "" OR $_POST["phone_search"] != "" OR $_POST["username_search"] != "" OR $_POST["position_search"] != "") {
	
		$Query = "SELECT user_id,user_name,user_surname,user_tel,user_username,position_id FROM user WHERE user_id != '2'  ";
	
	if($_POST["firstname_search"] != "") {
		$Query .= "AND user_name LIKE '%".$_POST["firstname_search"]."%' ";
	}
	if($_POST["lastname_search"] != "") {
		$Query .= "AND user_surname LIKE '%".$_POST["lastname_search"]."%' ";
	}
	if($_POST["phone_search"] != "") {
		$Query .= "AND user_tel LIKE '%".$_POST["phone_search"]."%' ";
	}
	if($_POST["username_search"] != "") {
		$Query .= "AND user_username LIKE '%".$_POST["username_search"]."%' ";
	}
	if($_POST["position_search"] != "") {
		$Query .= "AND position_id = '".$_POST["position_search"]."' ";
	}
	
} else {
	$Query = "SELECT user_id,user_name,user_surname,user_tel,user_username,position_id FROM user WHERE user_id != '2' ORDER BY user_name";
}
$objQuery = mysql_query($Query) or die ("Error Query [".$Query."]");
?>
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading">
      <h1>จัดการข้อมูลพนักงาน</h1>
    </div>
  <div class="content">
  <table class="list">
        <thead>
          <tr>
            <td style="text-align: center;"><span class="center">ชื่อ</span></td>
            <td class="center">นามสกุล</td>
            <td class="center">เบอร์โทรศัพท์</td>
            <td class="center">ชื่อผู้ใช้งานระบบ</td>
            <td class="center">ตำแหน่ง</td>
            <td class="center">จัดการสินค้า</td>
          </tr>
        </thead>
        <tbody>
		<form action="<?php echo $_SERVER["PHP_SELF"];?>" name="formsearch" method="post" id="formsearch">
          <tr class="filter">
            <td align="center"><input name="firstname_search" type="text" id="firstname_search" style="text-align: center;" /></td>
            <td align="center"><input name="lastname_search" type="text" id="lastname_search" style="text-align: center;" /></td>
            <td align="center"><input name="phone_search" type="text" id="phone_search" style="text-align: center;" /></td>
            <td align="center"><input name="username_search" type="text" id="username_search" style="text-align: center;" /></td>
            <td align="center"><select name="position_search">
            	<option value=""> เลือกตำแหน่ง </option>
            	<option value="1"> พนักงาน </option>
            	<option value="2"> ผู้จัดการ </option>
            </select></td>
            <td align="center"><a onclick="formsearch.submit();" class="button"><span>ค้นหา</span></a></td>
          </tr>
		</form>
<?php
	while($row = mysql_fetch_array($objQuery))
		{
?>
          <tr>
            <td class="left" style="padding-left:20px"><?php echo $row["user_name"];?></td>
            <td class="left" style="padding-left:20px"><?php echo $row["user_surname"];?></td>
            <td class="center"><?php echo $row["user_tel"];?></td>
            <td class="left" style="padding-left:20px"><?php echo $row["user_username"];?></td>
            <td class="left" style="padding-left:20px"><?php if($row["position_id"]=='1'){ echo "พนักงาน";} if($row["position_id"]=='2'){ echo "ผู้จัดการ";} ?></td>
            <td class="center">[ <a href="employee_edit.php?id=<?php echo $row["user_id"];?>">แก้ไข</a> ] [ <a href="JavaScript:if(confirm('คุณต้องการยืนยันการลบข้อมูล ใช่หรือไม่?')==true){window.location='<?php echo $_SERVER["PHP_SELF"];?>?action=del&id=<?php echo $row["user_id"];?>';}">ลบ</a> ]</td>
          </tr>
<?php
		} 
?>
        </tbody>
      </table>
  </div>
</div>

</body>
<?php mysql_close(); ?>
</html>
